﻿Imports System.IO
Imports Microsoft.Office.Interop.Excel

Public Class Spare

    Private Sub Spare_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        My.Settings.SparePos = Me.Location
        My.Settings.SpareSize = Me.Size


    End Sub

    Private Sub Spare_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CADISDataSet3.F1005' table. You can move, or remove it, as needed.
        Me.F1005TableAdapter1.Fill(Me.CADISDataSet3.F1005)

        Me.RibbonControl1.SelectedPage = RibbonPage1

        Me.Location = My.Settings.SparePos
        Me.Size = My.Settings.SpareSize

        'Load layout if exist
        If File.Exists(My.Settings.SpareLayout) Then
            PivotGridControl1.RestoreLayoutFromXml(My.Settings.SpareLayout)
            BarStaticItem1.Caption = "Loaded format: " & My.Settings.SpareLayout
        End If
    End Sub


    Private Sub BarButtonItem2_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick

        'Save pivot configuration
        Try
            Directory.CreateDirectory("c:\JLD004 Interface M3 Cadis")
        Catch ex As Exception
        End Try

        Dim saveFileDialog1 As System.Windows.Forms.SaveFileDialog
        saveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
        saveFileDialog1.FileName = "Preventiv spare parts"
        saveFileDialog1.InitialDirectory = "c:\JLD004 Interface M3 Cadis"
        saveFileDialog1.Filter = "JLD004 Interface M3 Cadis (*.jlds)|*.jlds"

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            PivotGridControl1.SaveLayoutToXml(saveFileDialog1.FileName)
        End If
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick

        'Load pivot configuration
        Dim openFileDialog1 As System.Windows.Forms.OpenFileDialog
        openFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        openFileDialog1.FilterIndex = 1
        openFileDialog1.InitialDirectory = "c:\JLD004 Interface M3 Cadis"
        openFileDialog1.Filter = "JLD004 Interface M3 Cadis (*.jlds)|*.jlds"

        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            PivotGridControl1.RestoreLayoutFromXml(openFileDialog1.FileName)
            My.Settings.SpareLayout = openFileDialog1.FileName
        End If

        BarStaticItem1.Caption = "Loaded format: " & openFileDialog1.FileName

    End Sub

    Private app As Application

    Private Sub BarButtonItem3_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick

        'Load data to excel
        Const filename As String = "c:\JLD004_Spare_Data.xls"

        If File.Exists(filename) Then
            File.Delete(filename)
        End If

        PivotGridControl1.ExportToXls(filename)

        app = New Application
        app.Visible = True

        app.Workbooks.Open(System.IO.Path.GetFullPath(filename))
    End Sub
End Class